package com.hontech.bv.ui.fragment.pipe_line

import android.view.View
import android.widget.Button
import android.widget.EditText
import com.hontech.bv.R
import com.hontech.bv.conn.Device
import com.hontech.bv.conn.api.PipeLine
import com.hontech.bv.task.appScope
import com.hontech.bv.task.onClick
import com.hontech.bv.ui.window.*
import com.hontech.bv.ui.wrapDialog
import com.hontech.bv.utils.toInt
import kotlinx.coroutines.launch

private fun showDropDialog() {
    ButtonDialog("落杯测试", arrayOf(
        ButtonKv("落杯1") {
            wrapDialog("落杯测试", "落杯1") {
                PipeLine.dropCup(1)
            }
        }, ButtonKv("落杯2") {
            wrapDialog("落杯测试", "落杯2") {
                PipeLine.dropCup(2)
            }
        }, ButtonKv("落杯1检测") {
            wrapDialog("落杯测试", "落杯1检测") {
                PipeLine.dropCup(3)
            }
        }, ButtonKv("落杯2检测") {
            wrapDialog("落杯测试", "落杯2检测") {
                PipeLine.dropCup(4)
            }
        })).show()
}

private fun showRotateDialog() {
    FormDialog("旋转测试", "测试", arrayOf(
        FormKV("脉冲数量", "0")
    )){
        val position = it[0]
        wrapDialog("旋转测试", "测试:${position}") {
            PipeLine.pipeLineCtrl(position)
        }
    }.show()
}

private fun showPickDialog() {
    FormDialog("出货", "出货", arrayOf(
        FormKV("落杯位置", "1"),
        FormKV("珍珠列号", "1"),
        FormKV("茶粉列号", "1"),
        FormKV("加水(ml)", "120"),
        FormKV("蒸汽时间", "30"),
        FormKV("出冰时间", "3"),
        FormKV("搅拌时间", "10"),
        FormKV("奶茶温度", "70")
    )){
        val cup = it[0]
        val pearl = it[1]
        val powder = it[2]
        val ml = it[3]
        val steam = it[4]
        val ice = it[5]
        val stir = it[6]
        val temp = it[7]
        wrapDialog("出货", "出货中") {
            PipeLine.pick(cup, pearl, powder, ml, steam, ice, stir, temp)
        }
    }.show()
}

private fun showLedOpenDialog() {
    FormDialog("LED打开", "打开", arrayOf(
        FormKV("LED ID[1,6]", "1")
    )){
        val id = it[0]
        wrapDialog("LED打开", "打开${id}号LED") {
            PipeLine.ledCtrl(id, 0)
        }
    }.show()
}

private fun showLedCloseDialog() {
    FormDialog("LED关闭", "关闭", arrayOf(
        FormKV("LED ID[1,6]", "1")
    )){
        val id = it[0]
        wrapDialog("LED关闭", "关闭${id}号LED") {
            PipeLine.ledCtrl(id, 1)
        }
    }.show()
}

class CtrlDelegate (view: View) {

    private val btnDrop = view.findViewById<Button>(R.id.id_pl_btn_drop_cup)
    private val btnRotate = view.findViewById<Button>(R.id.id_pl_btn_rotate_ctrl)
    private val btnPick = view.findViewById<Button>(R.id.id_pl_btn_pick)

    private val btnElock = view.findViewById<Button>(R.id.id_pl_btn_elock)
    private val btnLedOpen = view.findViewById<Button>(R.id.id_pl_btn_led_open)
    private val btnLedClose = view.findViewById<Button>(R.id.id_pl_btn_led_close)

    init {
        btnDrop.setOnClickListener { showDropDialog() }
        btnRotate.setOnClickListener { showRotateDialog() }
        btnPick.setOnClickListener { showPickDialog() }
        btnLedOpen.setOnClickListener { showLedOpenDialog() }
        btnLedClose.setOnClickListener { showLedCloseDialog() }
        btnElock.onClick {
            wrapDialog("门锁打开", "执行中") {
                PipeLine.eLockOpen()
            }
        }
    }




}